# To change this template, choose Tools | Templates
# and open the template in the editor.
from game.ai.action import Action
from game.fov import *

class MoveAction(Action):
    def __init(self):
        Action.__init__(self)

    def perform(self, dat, src, dst):
        #source tile
        s_tile = dat.level.tiles[src[0]][src[1]]
        if s_tile is None or s_tile.occupant is None:
            return None

        #print src, dst
        nx = src[0] + dst[0]
        ny = src[1] + dst[1]

        #destination tile
        d_tile = dat.level.tiles[nx][ny]

        if d_tile.is_path_blocked():
#            if d_tile.context == 'door_closed':
#                return 'bump'
            return None
        elif d_tile.occupant is None:

            c = s_tile.occupant
            d_tile.occupant = c
            s_tile.occupant = None

            c.x, c.y = [nx, ny]
            c.rect.topleft = (nx * dat.sprite_pitch[0], ny * dat.sprite_pitch[1])

            if c == dat.main_char:
                dat.level.apply_fog()
                do_fov(dat.level, c.pos(), 10)
                dat.act_mgr.perform("check", dat, c.pos(), [(0,0)])
                dat.gui.game_area.adjust_view()


            #print 'rep on {0}'.format(c.name)
            dat.sched_mgr.rep_char(c)
            return None
        else:
            return 'attack'

